<template>
  <div>
    <el-form inline class="mt-10" :model="search">
      <el-form-item label="终端SN码">
        <el-select v-model="search.sn" clearable filterable placeholder="请输入或选择">
          <el-option v-for="item in sn_list" :key="item.device_sn" :value="item.device_sn" :label="item.device_sn"></el-option>
        </el-select>
      </el-form-item>
      <el-button type="success" size="small" icon="el-icon-search" @click="handleClickSearch">查询</el-button>
    </el-form>
    <el-table size="small" :data="date">
      <el-table-column align="center" label="终端SN码" prop="speakerid"></el-table-column>
      <el-table-column align="center" label="用户标识" prop="userid"></el-table-column>
      <el-table-column align="center" label="绑定状态">
        <template slot-scope="scope">
          <span>{{ scope.row.active == 1 ? '已绑定' : '未绑定' }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="绑定时间" prop="datetime"></el-table-column>
      <el-table-column align="center" label="音量值">
        <template slot-scope="scope">
          <el-button type="text" class="success" @click="handleSettingVolume(scope.row.ID, scope.row.speakerid, scope.row.volume)">{{ scope.row.volume }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="handleBroadcastStatus(scope.row.ID, scope.row.speakerid)">播报状态</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="fr mt-10" :total="page.total" :current-page.sync="page.current" :page-size="page.size" layout="total, prev, pager, next, jumper" @current-change="handleChangePage"></el-pagination>
    <el-dialog :visible.sync="volume" width="400px" title="声音设置">
      <el-form :model="vol" label-width="60px" style="padding-top:20px;" ref="volume" :rules="rules">
        <el-form-item prop="value" label="音量值">
          <el-input type="number" placeholder="请输入音量值" v-model="vol.value"></el-input>
          <span style="color:red;">音量值请设置为0-100的整数</span>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="primary" size="small" @click="handleClickSaveVolume">保存设置</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="broadcasts" width="400px" title="播报状态">
      <p>音量值：{{ broadcast.volume }}</p>
      <p>支付金额：{{ broadcast.price }}</p>
      <p>支付类型：{{ broadcast.price_type }}</p>
    </el-dialog>
  </div>
</template>

<script>
import { CloudSpearkStatusInit, CloudSpearkStatusSearch, CloudSpearkStatusPage, CloudSpearkStatusVolume, CloudSpearkStatusGetvol } from '@/api/shop'

export default {
  data() {
    return {
      volume: false,
      broadcasts: false,
      sn_list: [],
      date: [],
      search: {
        sn: ''
      },
      page: {
        size: 0,
        current: 1,
        total: 0
      },
      vol: {
        id: '',
        sn: '',
        value: 0
      },
      broadcast: {
        price: '',
        volume: '',
        price_type: ''
      },
      rules: {
        value: [{ required: true, trigger: 'blur', message: '必须填写哦~' }]
      }
    }
  },
  methods: {
    handleDocumentInit() {
      CloudSpearkStatusInit(this.$route.query.store_id).then(res => {
        if (res.data.errorCode === 0) {
          this.date = res.data.result.list
          this.page.size = res.data.result.list_count
          this.page.total = res.data.result.total_count
          this.page.current = res.data.result.page
          this.sn_list = res.data.result.sn
        } else {
          this.$message.error(res.data.errorMsg)
        }
      })
    },
    handleClickSearch() {
      CloudSpearkStatusSearch(this.$route.query.store_id, this.search).then(res => {
        if (res.data.errorCode === 0) {
          this.date = res.data.result.list
          this.page.size = res.data.result.list_count
          this.page.total = res.data.result.total_count
          this.page.current = res.data.result.page
          this.sn_list = res.data.result.sn
        } else {
          this.$message.error(res.data.errorMsg)
        }
      })
    },
    handleChangePage(v) {
      CloudSpearkStatusPage(this.$route.query.store_id, this.search, v).then(res => {
        if (res.data.errorCode === 0) {
          this.date = res.data.result.list
          this.page.size = res.data.result.list_count
          this.page.total = res.data.result.total_count
          this.page.current = res.data.result.page
          this.sn_list = res.data.result.sn
        } else {
          this.$message.error(res.data.errorMsg)
        }
      })
    },
    handleSettingVolume(id, sn, vol) {
      this.vol.id = id
      this.vol.sn = sn
      this.vol.value = vol
      this.volume = true
    },
    handleClickSaveVolume() {
      this.$refs['volume'].validate(valid => {
        if (valid) {
          CloudSpearkStatusVolume(this.vol.id, this.vol.sn, this.vol.value).then(res => {
            if (res.data.errorCode === 0) {
              this.$message.success(res.data.errorMsg)
              CloudSpearkStatusPage(this.$route.query.store_id, this.search, this.page.current).then(res => {
                if (res.data.errorCode === 0) {
                  this.date = res.data.result.list
                  this.page.size = res.data.result.list_count
                  this.page.total = res.data.result.total_count
                  this.page.current = res.data.result.page
                  this.sn_list = res.data.result.sn
                } else {
                  this.$message.error(res.data.errorMsg)
                }
              })
              this.volume = false
            } else {
              this.$message.error(res.data.errorMsg)
            }
          })
        }
      })
    },
    handleBroadcastStatus(uid, sn) {
      CloudSpearkStatusGetvol(uid, sn).then(res => {
        if (res.data.errorCode === 0) {
          this.broadcast.price = res.data.result.data.price
          this.broadcast.volume = res.data.result.data.volume
          this.broadcast.price_type = res.data.result.data.price_type
          this.broadcasts = true
        } else {
          this.$message.error(res.data.errorMsg)
        }
      })
    }
  },
  created() {
    this.handleDocumentInit()
  }
}
</script>